package com.lbh.cloudgallery.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lbh.cloudgallery.entity.AuthorEntity;
import com.lbh.cloudgallery.entity.ProductEntity;
import com.lbh.cloudgallery.service.AuthorService;
import com.lbh.cloudgallery.service.ProductService;
import com.xxx.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "前端-作者模块")
@RestController
@RequestMapping("front/author")
public class FrontAuthorController {

    @Autowired
    private AuthorService authorService;

    @Autowired
    private ProductService productService;

    //分页查询作者
    @ApiOperation(value="分页查询作者")
    @PostMapping("getAuthorList/{page}/{limit}")
    public R getAuthorList(@PathVariable long page,@PathVariable long limit){

        Page<AuthorEntity> pageAuthor=new Page<>(page,limit);
        Map<String,Object> map=authorService.getAuthorList(pageAuthor);

        //返回分页所有数据
        return R.ok().setData(map);
    }

    //根据作者name查询作品
    @ApiOperation(value="根据作者name查询作品")
    @GetMapping("getAuthorInfo/{name}")
    public R getAuthorInfo(@PathVariable String name){
        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("author",name);
        List<ProductEntity> ProductList=productService.list(wrapper);

        return R.ok().setData(ProductList);
    }

    @ApiOperation(value="根据作者名，模糊查询作者")
    @GetMapping("getAuthorByName/{name}")
    public R getAuthorByName(@PathVariable String name){
        QueryWrapper<AuthorEntity> wrapper = new QueryWrapper<>();
        wrapper.like("name",name);
        List<AuthorEntity> list = authorService.list(wrapper);

        return R.ok().setData(list);
    }

    //查询所有作者
    @ApiOperation(value="查询所有作者")
    @GetMapping("getAllAuthor")
    public R getAllAuthor(){
        List<AuthorEntity> authorEntityList=authorService.list(null);
        return R.ok().setData(authorEntityList);
    }
}
